package com.utils;

public class CarVin {
    /**
     * 车架号地区代码数组
     */
    private static final String areaArray[] = new String[]{"1", "2", "3", "6", "9", "J", "K", "L", "R", "S", "T", "V", "W", "Y", "Z", "G"};

    /**
     * 车架号中可能出现的字符数组
     */
    private static final String charArray[] = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "V", "W", "X", "Y"};


    /**
     * 拼接车架号
     *
     * @param beforeStr
     * @param afterStr
     * @return
     */
    public String spellVin(String beforeStr, String afterStr) {
        StringBuffer vinBuffer = new StringBuffer();
        String preVin = vinBuffer.append(beforeStr).append("X").append(afterStr).toString();
        return preVin;
    }

    /**
     * 生成随机前缀
     *
     * @return
     */
    public String prepareBeforeStr() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("VNN");
        for (int i = 0; i < 5; i++) {
            String a = getRandomChar(areaArray);
            stringBuffer.append(a);
        }
        return stringBuffer.toString();
    }

    /**
     * 生成随机后缀
     *
     * @return
     */
    public String prepareAfterStr() {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 4; i++) {
            stringBuffer.append(getRandomChar(charArray));
        }
        stringBuffer.append(prepareNo());
        return stringBuffer.toString();
    }

    /**
     * 生成随机的生产序号
     *
     * @return
     */
    public String prepareNo() {
        StringBuffer numStrBuff = new StringBuffer();
        numStrBuff.append((int) (Math.random() * 9000) + 1000);
        return numStrBuff.toString();
    }

    /**
     * 返回随机字符
     *
     * @return
     */
    public String getRandomChar(Object array[]) {
        return charArray[(int) (Math.random() * 100 % array.length)];
    }

    public String getRandomVIN() {
        String beforeStr = prepareBeforeStr();
        String afterStr = prepareAfterStr();
        String vin = spellVin(beforeStr, afterStr);
        return vin;
    }

    public String getGPSNum() {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 5; i++) {
            stringBuffer.append(getRandomChar(charArray));
            if (i % 2 == 0) {
                stringBuffer.append(prepareNo());
            }
        }
        return stringBuffer.toString();
    }

}
